# coding=utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
from application import app
import json




# ALIYUN_SMS = {
#     'region' : "cn-beijing",
#     'access_key_id' : "LTAI6IogtZ6Dspxr",
#     'access_key_secret' : "4WuyfDqX0WOc67vNvpq8TWOILvjOOc"
# }
# acs_client = AcsClient( ALIYUN_SMS["access_key_id"], ALIYUN_SMS["access_key_secret"], ALIYUN_SMS["region"])

class AliyunLibs():
    def __init__(self):
        self.acs_client = AcsClient( app.config["ALIYUN_SMS"]["access_key_id"], app.config["ALIYUN_SMS"]["access_key_secret"], app.config["ALIYUN_SMS"]["region"])

    def sendMsg(self, PhoneNumbers, code) :
        request = CommonRequest()
        request.set_accept_format('json')
        request.set_domain('dysmsapi.aliyuncs.com')
        request.set_method('POST')
        request.set_protocol_type('https') # https | http
        request.set_version('2017-05-25')
        request.set_action_name('SendSms')

        request.add_query_param('PhoneNumbers', PhoneNumbers)
        request.add_query_param('SignName', '廿加')
        request.add_query_param('TemplateCode', 'SMS_161265187')
        request.add_query_param('TemplateParam', '{"code":"' + code + '"}')
        response = self.acs_client.do_action_with_exception(request)
        return json.loads(str(response, encoding = 'utf-8'))

    def queryMsg(self, PhoneNumbers, send_date) :
        request = CommonRequest()
        request.set_accept_format('json')
        request.set_domain('dysmsapi.aliyuncs.com')
        request.set_method('POST')
        request.set_protocol_type('https')  # https | http
        request.set_version('2017-05-25')
        request.set_action_name('QuerySendDetails')

        request.add_query_param('PageSize', '1')
        request.add_query_param('CurrentPage', '1')
        request.add_query_param('SendDate', send_date)
        request.add_query_param('PhoneNumber', PhoneNumbers)

        response = self.acs_client.do_action_with_exception(request)
        return json.loads(str(response, encoding = 'utf-8'))
